//
//  SmallDecisionsHUDHelper.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/6/14.
//

#import "SmallDecisionsHUDHelper.h"
#import <MBProgressHUD/MBProgressHUD.h>

@implementation SmallDecisionsHUDHelper
+ (void)showLoadingInview:(UIView *)view
                  addMask:(BOOL)addMask {
    if (!view)
        view = [SmallDecisionsHUDHelper getKeyWindow];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    if (!addMask) {
        
        [hud mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.center.mas_equalTo(view);
            make.width.mas_lessThanOrEqualTo(view.mas_width).mas_offset(60);
            make.height.mas_lessThanOrEqualTo(view.mas_height).mas_offset(60);
        }];
    }
    // 设置模式
    hud.mode = MBProgressHUDModeIndeterminate;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.color = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.contentColor = [UIColor whiteColor];
    hud.userInteractionEnabled = YES;
}

/**
*
*  @param view 显示的视图
*/
+ (void)showLoadingInview:(UIView *)view {
    
    if (!view)
        view = [SmallDecisionsHUDHelper getKeyWindow];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    // 设置模式
    hud.mode = MBProgressHUDModeIndeterminate;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.color = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.contentColor = [UIColor whiteColor];
    hud.userInteractionEnabled = YES;
}

+ (void)showLoading:(NSString *)text
               view:(UIView * _Nullable)view
            addMask:(BOOL)addMask {
    
    if (!view)
        view = [SmallDecisionsHUDHelper getKeyWindow];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    if (!addMask) {
        
        [hud mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.center.mas_equalTo(view);
            make.width.mas_lessThanOrEqualTo(view.mas_width).mas_offset(60);
            make.height.mas_lessThanOrEqualTo(view.mas_height).mas_offset(60);
        }];
    }
    hud.contentColor = [UIColor whiteColor];
    hud.label.text = text;
    // 设置模式
    hud.mode = MBProgressHUDModeIndeterminate;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.color = [UIColor blackColor];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.userInteractionEnabled = YES;
}

/**
 *  显示信息
 *
 *  @param text 信息内容
 *  @param view 显示的视图
 */
+ (void)showLoading:(NSString * _Nullable)text view:( UIView * _Nullable )view {
    
    if (!view)
        view = [SmallDecisionsHUDHelper getKeyWindow];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.contentColor = [UIColor whiteColor];
    hud.label.text = text;
    // 设置模式
    hud.mode = MBProgressHUDModeIndeterminate;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.color = [UIColor blackColor];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.userInteractionEnabled = YES;
}

/**
 *  显示信息
 *
 *  @param text 信息内容
 *  @param icon 图标
 *  @param view 显示的视图
 */
+ (void)show:(NSString *)text
        icon:(NSString *)icon
        view:(UIView *)view
     addMask:(BOOL)addMask {
    
    if (!view) {
        
        view = [SmallDecisionsHUDHelper getKeyWindow];
    }
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    if (!addMask) {
        
        [hud mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.center.mas_equalTo(view);
            make.width.mas_lessThanOrEqualTo(view.mas_width).mas_offset(60);
            make.height.mas_lessThanOrEqualTo(view.mas_height).mas_offset(60);
        }];
    }
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: icon]];
    hud.label.text = text;
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.margin = 15;
    [hud setAnimationType:MBProgressHUDAnimationZoomOut];
//    [hud setOffset:CGPointMake(0, MBProgressMaxOffset)];
    hud.bezelView.color = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.contentColor = [UIColor whiteColor];
    // 3秒之后再消失
    [hud hideAnimated:YES afterDelay: 2.0];
    hud.userInteractionEnabled = YES;
}

+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view {
    
    if (!view) {
        
        view = [SmallDecisionsHUDHelper getKeyWindow];
    }
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: icon]];
    hud.label.text = text;
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.margin = 15;
    [hud setAnimationType:MBProgressHUDAnimationZoomOut];
//    [hud setOffset:CGPointMake(0, MBProgressMaxOffset)];
    hud.bezelView.color = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.contentColor = [UIColor whiteColor];
    // 3秒之后再消失
    [hud hideAnimated:YES afterDelay: 2.0];
    hud.userInteractionEnabled = YES;
}

/**
 *  显示成功信息
 *
 *  @param success 信息内容
 */
+ (void)showSuccess:(NSString *)success {
    
    [self showSuccess:success toView:nil];
}

/**
 *  显示成功信息
 *
 *  @param success 信息内容
 *  @param view    显示信息的视图
 */
+ (void)showSuccess:(NSString *)success
             toView:(nullable UIView *)view {
    
    [self show:success icon:@"success_icon" view:view addMask:NO];
}

/**
 *  显示错误信息
 *
 */
+ (void)showError:(NSString *)error {
    
    [self showError:error toView:nil];
}

/**
 *  显示错误信息
 *
 *  @param error 错误信息内容
 *  @param view  需要显示信息的视图
 */
+ (void)showError:(NSString *)error
           toView:(nullable UIView *)view {
    
    [self show:error icon:@"error_icon" view:view addMask:NO];
}

/**
 *  显示错误信息
 *
 *  @param message 信息内容
 */
+ (void)showMessage:(NSString *)message {
    
    return [self showMessage:message toView: nil];
}

/**
 *  显示一些信息
 *
 *  @param message 信息内容
 *  @param view    需要显示信息的视图
 */
+ (void)showMessage:(NSString *)message
             toView:(UIView *)view
            addMask:(BOOL)addMask {
    
    if (!view) view = [SmallDecisionsHUDHelper getKeyWindow];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    if (!addMask) {
        
        [hud mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.center.mas_equalTo(view);
            make.width.mas_lessThanOrEqualTo(view.mas_width).mas_offset(60);
            make.height.mas_lessThanOrEqualTo(view.mas_height).mas_offset(60);
        }];
    }
    hud.label.text = message;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.mode = MBProgressHUDModeText;
    hud.margin = 15;
    [hud setAnimationType:MBProgressHUDAnimationZoomIn];
//    [hud setOffset:CGPointMake(0, MBProgressMaxOffset)];
    hud.bezelView.color = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.contentColor = [UIColor whiteColor];
    // 2秒之后再消失
    [hud hideAnimated:YES afterDelay:2.0];
    hud.userInteractionEnabled = YES;
}

+ (void)showMessage:(NSString *)message
             toView:(UIView *)view {
    
    if (!view) view = [SmallDecisionsHUDHelper getKeyWindow];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = message;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.mode = MBProgressHUDModeText;
    hud.margin = 15;
    [hud setAnimationType:MBProgressHUDAnimationZoomIn];
//    [hud setOffset:CGPointMake(0, MBProgressMaxOffset)];
    hud.bezelView.color = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.contentColor = [UIColor whiteColor];
    // 2秒之后再消失
    [hud hideAnimated:YES afterDelay:2.0];
    hud.userInteractionEnabled = YES;
}

/**
 *  手动关闭MBProgressHUD
 */
+ (void)hideHUD {
    
    [self hideHUDForView: nil];
}

/**
 *  手动关闭MBProgressHUD
 *
 *  @param view    显示MBProgressHUD的视图
 */
+ (void)hideHUDForView:(nullable UIView *)view {
    
    if (!view)
        view = [SmallDecisionsHUDHelper getKeyWindow];
    
    [MBProgressHUD hideHUDForView:view animated:YES];
}


+ (UIWindow *)getKeyWindow {
    
    if ( @available(iOS 13.0, *) ) {
        for ( UIScene *scene in UIApplication.sharedApplication.connectedScenes ) {
            if ( [scene isKindOfClass:UIWindowScene.class] ) {
                return [(UIWindowScene *)scene windows].firstObject;
            }
        }
    }
    return [UIApplication sharedApplication].delegate.window;
}

@end
